﻿Public Class ConnectConfigForm

    Private _parameters() As String

    Private Sub ConnectConfigForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToParent()

        For i As Integer = 0 To ctrls.Length - 1
            Dim p As String = _parameters(i)
            If p IsNot Nothing Then
                ctrls(i).Text = p
            End If
        Next
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        For i As Integer = 0 To ctrls.Length - 1
            _parameters(i) = ctrls(i).Text
        Next
        'msg.Show("保存成功")
        Me.DialogResult = Windows.Forms.DialogResult.OK
        'Me.Close()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        'Me.Close()
    End Sub

    Private Function CheckValidate() As Boolean
        '检查数值是否为空, 是否输入正确
        For Each txt As TextBox In ctrls
            If txt.TextLength <= 0 Then
                msg.ShowWarning("数值不能为空")
                txt.SelectAll()
                txt.Focus()
                Return False
            Else
                Dim value As Byte
                Try
                    value = CByte(txt.Text)
                Catch ex As Exception
                    msg.ShowWarning("数值输入错误，应在0－255之间")
                    txt.SelectAll()
                    txt.Focus()
                    Return False
                End Try
            End If
        Next

        Return True
    End Function

    Dim ctrls(2) As TextBox
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        ctrls(0) = Me.txtIdleTime
        ctrls(1) = Me.txtWaitTime
        ctrls(2) = Me.txtRep
    End Sub
    Public Sub New(ByVal parameters() As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        ctrls(0) = Me.txtIdleTime
        ctrls(1) = Me.txtWaitTime
        ctrls(2) = Me.txtRep

        _parameters = parameters
    End Sub

    Private Sub txtIdleTime_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIdleTime.KeyPress, txtWaitTime.KeyPress, txtRep.KeyPress
        e.Handled = False
        If Not System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar, "^\d$") AndAlso Asc(e.KeyChar) <> Keys.Back AndAlso Asc(e.KeyChar) <> Keys.Delete Then
            e.Handled = True
        End If
    End Sub
End Class